import 'package:flutter/material.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:xinxuan/common/network_image_util.dart';
import 'package:xinxuan/common/store_order_btns.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/controllers/store_order_controller/store_order_page_controller.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:xinxuan/utils/refresh_util.dart';
import 'package:xinxuan/vo/store_order_list_vo.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class StoreOrderGoodItem extends StatefulWidget {

  final StoreOrderListVo storeOrderListVo;
  final StoreOrderPageController storeOrderPageController;
  StoreOrderGoodItem({required this.storeOrderListVo, required this.storeOrderPageController});

  @override
  _StoreOrderGoodItemState createState() => _StoreOrderGoodItemState();
}

class _StoreOrderGoodItemState extends State<StoreOrderGoodItem> with AutomaticKeepAliveClientMixin {

  final CommonPageController commonPageController = Get.find();

  @override
  Widget build(BuildContext context) {
    super.build(context);
    if (widget.storeOrderListVo.data!.length <= 0) {
      return Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image.asset(
              'assets/icons/icon_order_list_def.png',
              width: 273.w,
              height: 146.h,
            ),
            SizedBox(height: 10.h),
            Text(
              '还没订单~去下一单！',
              style: TextStyle(
                color: textSubtitleColor,
                fontSize: 14.sp
              ),
            )
          ],
        ),
      );
    }
    return RefreshUtil.easyRefresh(
      onRefresh: () async {
        widget.storeOrderPageController.getStoreOrderList();
      },
      onLoad: () async {
        widget.storeOrderPageController.onloadStoreOrderList();
      },
      slivers: [
        SliverToBoxAdapter(
          child: commonPageController.userData.value.isWriteoffOfficer == 1 && widget.storeOrderPageController.tabIndex.value == 2 ? Container(
            margin: EdgeInsets.only(bottom: 12.h),
            padding: EdgeInsets.symmetric(horizontal: 22.w),
            child: ButtonUtil(
              text: '一键全部核销', 
              fontSize: 14.sp, 
              borderRadius: 5.w, 
              height: 45.h, 
              bgColors: [
                bgOrderCheckoff,
                bgOrderCheckoff
              ],
              onTop: () {
                widget.storeOrderPageController.orderCheckoff();
              }
            ),
          ) : Container(),
        ),
        SliverList(
          delegate: SliverChildBuilderDelegate((context, index) {
            StoreOrderItem item = widget.storeOrderListVo.data![index];
            return InkWellUtil.shakeProofInkWell(Container(
              margin: EdgeInsets.only(bottom: 12.h),
              padding: EdgeInsets.symmetric(horizontal: 16.w),
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 8.w),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(5.w),
                  color: bgWhiteColor
                ),
                child: Column(
                  children: [
                    Container(
                      height: 44.h,
                      padding: EdgeInsets.symmetric(horizontal: 4.w),
                      decoration: BoxDecoration(
                        border: Border(
                          bottom: BorderSide(
                            width: .5,
                            color: lineColor
                          )
                        )
                      ),
                      child: Row(
                        children: [
                          Expanded(child: Text(
                            '订单号:  ${item.orderSn}',
                            overflow: TextOverflow.ellipsis,
                            maxLines: 1,
                            style: TextStyle(
                              color: textBlackColor,
                              fontSize: 14.sp,
                              fontWeight: FontWeight.bold
                            ),
                          )),
                          Text(
                            '${item.statusStr}',
                            style: TextStyle(
                              color: mainTextColor,
                              fontSize: 14.sp
                            ),
                          )
                        ],
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.symmetric(horizontal: 4.w, vertical: 12.h),
                      child: Column(
                        children: [
                          Column(
                            children: item.products!.map((productsItem) {
                              return Container(
                                margin: EdgeInsets.only(bottom: 12.h),
                                child: Row(
                                  children: [
                                    NetworkImageUtil(
                                      url: productsItem.image ?? '', 
                                      width: 76.w, 
                                      height: 76.w,
                                      borderRadius: 5.w,
                                    ),
                                    SizedBox(width: 12.w),
                                    Expanded(child: Container(
                                      constraints: BoxConstraints(
                                        minHeight: 76.w
                                      ),
                                      child: Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: [
                                          Row(
                                            children: [
                                              Expanded(child: Text(
                                                '${productsItem.title}',
                                                overflow: TextOverflow.ellipsis,
                                                maxLines: 2,
                                                style: TextStyle(
                                                  color: textBlackColor,
                                                  fontSize: 14.sp
                                                ),
                                              )),
                                              SizedBox(width: 12.w),
                                              Text(
                                                'x${productsItem.quantity}',
                                                style: TextStyle(
                                                  color: textSubtitleColor,
                                                  fontSize: 12.sp
                                                ),
                                              )
                                            ],
                                          ),
                                          SizedBox(height: 12.w),
                                          Text(
                                            '${item.createdAt}',
                                            style: TextStyle(
                                              color: textSubtitleColor,
                                              fontSize: 12.sp
                                            ),
                                          )
                                        ],
                                      ),
                                    )),
                                  ],
                                ),
                              );
                            }).toList(),
                          ),
                          Container(
                            alignment: Alignment.centerRight,
                            child: Text.rich(
                              TextSpan(
                                text: '合计支付:  ',
                                style: TextStyle(
                                  fontWeight: FontWeight.bold,
                                  fontSize: 14.sp,
                                  color: textBlackColor
                                ),
                                children: [
                                  TextSpan(
                                    text: '¥',
                                    style: TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 12.sp,
                                      color: textBlackColor
                                    ),
                                  ),
                                  TextSpan(
                                    text: '${item.totalAmount}',
                                    style: TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16.sp,
                                      color: textBlackColor
                                    ),
                                  )
                                ]
                              )
                            ),
                          )
                        ],
                      ),
                    ),
                    item.status! == 1 || (item.status! == 2 && item.pickType == 1) ? Container(
                      height: 60.h,
                      padding: EdgeInsets.symmetric(horizontal: 4.w),
                      decoration: BoxDecoration(
                        border: Border(
                          top: BorderSide(
                            color: lineColor,
                            width: .5
                          )
                        )
                      ),
                      child: StoreOrderBtns( 
                        status: item.status!, 
                        pickType: item.pickType!,
                        voucherCode: item.voucherCode,
                        isDetail: false,
                        transferRemaining: item.transferRemaining ?? 0,
                        isWriteoffOfficer: commonPageController.userData.value.isWriteoffOfficer ?? 0,
                        type: item.type ?? 1,
                        isTransfer: item.isTransfer ?? 0,
                        toPayFun: () async {
                          var res = await Get.toNamed(
                            '${AppRouters.COMMON_PAGE}${AppRouters.GOOD_DETAIL_PAGE}${AppRouters.STORE_CONFIRM_ORDER_PAGE}${AppRouters.STORE_ORDER_PAY_PAGE}',
                            arguments: {
                              'orderId': item.id,
                              'isDetail': false,
                              'isOrderPage': true
                            }
                          );
                          if (res != null && res) {
                            widget.storeOrderPageController.getStoreOrderList();
                          }
                        },
                        cleanOrderFun: () {
                          widget.storeOrderPageController.storeOrderCancel(item.id!);
                        },
                        receiveOrderFun: () {},
                        logisticsFun: () {},
                        confirmReceiptFun: () {},
                        conversionPickingFun: () {
                          if (item.transferRemaining! > 0) {
                            Get.toNamed(
                              '${AppRouters.COMMON_PAGE}${AppRouters.STORE_ORDER_PAGE}${AppRouters.STORE_ORDER_DETAIL_PAGE}',
                              arguments: {
                                'orderId': item.id,
                                'title': '${item.statusStr}',
                                'status': item.status,
                                'isOrderPage': true,
                                'storeOrderPageController': widget.storeOrderPageController,
                              }
                            );
                            return;
                          }
                          List<Map<String, dynamic>> mapArr = [];
                          item.products!.forEach((item) {
                            Map<String, dynamic> map = {
                              'image': item.image,
                              'title': item.title,
                              'price': item.price,
                              'quantity': item.quantity
                            };
                            mapArr.add(map);
                          });
                          widget.storeOrderPageController.conversionPickingFun(
                            products: mapArr,
                            orderId: item.id!,
                            status: item.status!,
                            statusStr: item.statusStr ?? '',
                            isDetail: false
                          );
                        },
                        pickUpCodeFun: () {
                          List<Map<String, dynamic>> mapArr = [];
                          item.products!.forEach((item) {
                            Map<String, dynamic> map = {
                              'image': item.image,
                              'title': item.title,
                              'price': item.price,
                              'quantity': item.quantity
                            };
                            mapArr.add(map);
                          });
                          widget.storeOrderPageController.pickUpCodeFun(
                            products: mapArr,
                            orderId: item.id!,
                            status: item.status!,
                            statusStr: item.statusStr ?? ''
                          );
                        },
                        seePickUpCodeFun: () {
                          Get.toNamed(
                            '${AppRouters.COMMON_PAGE}${AppRouters.STORE_ORDER_PAGE}${AppRouters.STORE_ORDER_DETAIL_PAGE}',
                            arguments: {
                              'orderId': item.id,
                              'title': '${item.statusStr}',
                              'status': item.status,
                              'isOrderPage': true,
                              'storeOrderPageController': widget.storeOrderPageController,
                            }
                          );
                        },
                        checkoff: () {
                          widget.storeOrderPageController.checkoff(item.id!);
                        },
                      )
                    ) : Container(
                      height: 60.h,
                      padding: EdgeInsets.symmetric(horizontal: 4.w),
                      decoration: BoxDecoration(
                        border: Border( 
                          top: BorderSide(
                            color: lineColor,
                            width: .5
                          )
                        )
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          Container(
                            width: 90.w,
                            child: ButtonUtil(
                              text: '查看订单', 
                              fontSize: 14.w, 
                              borderRadius: 18.w, 
                              height: 36.h, 
                              onTop: () {
                                Get.toNamed(
                                  '${AppRouters.COMMON_PAGE}${AppRouters.STORE_ORDER_PAGE}${AppRouters.STORE_ORDER_DETAIL_PAGE}',
                                  arguments: {
                                    'orderId': item.id,
                                    'title': '${item.statusStr}',
                                    'status': item.status,
                                    'isOrderPage': true,
                                    'storeOrderPageController': widget.storeOrderPageController,
                                  }
                                );
                              }
                            ),
                          )
                        ],
                      )
                    )
                  ],
                ),
              ),
            ), () {
              Get.toNamed(
                '${AppRouters.COMMON_PAGE}${AppRouters.STORE_ORDER_PAGE}${AppRouters.STORE_ORDER_DETAIL_PAGE}',
                arguments: {
                  'orderId': item.id,
                  'title': '${item.statusStr}',
                  'status': item.status,
                  'isOrderPage': true,
                  'storeOrderPageController': widget.storeOrderPageController,
                }
              );
            });
          }, childCount: widget.storeOrderListVo.data!.length),
        ),
      ]
      // slivers: widget.storeOrderListVo.data!.map((item) {
      //   return SliverToBoxAdapter(
      //     child: InkWellUtil.shakeProofInkWell(Container(
      //       margin: EdgeInsets.only(bottom: 12.h),
      //       padding: EdgeInsets.symmetric(horizontal: 16.w),
      //       child: Container(
      //         padding: EdgeInsets.symmetric(horizontal: 8.w),
      //         decoration: BoxDecoration(
      //           borderRadius: BorderRadius.circular(5.w),
      //           color: bgWhiteColor
      //         ),
      //         child: Column(
      //           children: [
      //             Container(
      //               height: 44.h,
      //               padding: EdgeInsets.symmetric(horizontal: 4.w),
      //               decoration: BoxDecoration(
      //                 border: Border(
      //                   bottom: BorderSide(
      //                     width: .5,
      //                     color: lineColor
      //                   )
      //                 )
      //               ),
      //               child: Row(
      //                 children: [
      //                   Expanded(child: Text(
      //                     '订单号:  ${item.orderSn}',
      //                     overflow: TextOverflow.ellipsis,
      //                     maxLines: 1,
      //                     style: TextStyle(
      //                       color: textBlackColor,
      //                       fontSize: 14.sp,
      //                       fontWeight: FontWeight.bold
      //                     ),
      //                   )),
      //                   Text(
      //                     '${StoreOrderStatusUtil.statusMap['${item.status}']}',
      //                     style: TextStyle(
      //                       color: mainTextColor,
      //                       fontSize: 14.sp
      //                     ),
      //                   )
      //                 ],
      //               ),
      //             ),
      //             Container(
      //               padding: EdgeInsets.symmetric(horizontal: 4.w, vertical: 12.h),
      //               child: Column(
      //                 children: [
      //                   Column(
      //                     children: item.products!.map((productsItem) {
      //                       return Container(
      //                         margin: EdgeInsets.only(bottom: 12.h),
      //                         child: Row(
      //                           children: [
      //                             NetworkImageUtil(
      //                               url: productsItem.image ?? '', 
      //                               width: 76.w, 
      //                               height: 76.w,
      //                               borderRadius: 5.w,
      //                             ),
      //                             SizedBox(width: 12.w),
      //                             Expanded(child: Container(
      //                               constraints: BoxConstraints(
      //                                 minHeight: 76.w
      //                               ),
      //                               child: Column(
      //                                 crossAxisAlignment: CrossAxisAlignment.start,
      //                                 children: [
      //                                   Row(
      //                                     children: [
      //                                       Expanded(child: Text(
      //                                         '${productsItem.title}',
      //                                         overflow: TextOverflow.ellipsis,
      //                                         maxLines: 2,
      //                                         style: TextStyle(
      //                                           color: textBlackColor,
      //                                           fontSize: 14.sp
      //                                         ),
      //                                       )),
      //                                       SizedBox(width: 12.w),
      //                                       Text(
      //                                         'x${productsItem.quantity}',
      //                                         style: TextStyle(
      //                                           color: textSubtitleColor,
      //                                           fontSize: 12.sp
      //                                         ),
      //                                       )
      //                                     ],
      //                                   ),
      //                                   SizedBox(height: 12.w),
      //                                   Text(
      //                                     '${item.createdAt}',
      //                                     style: TextStyle(
      //                                       color: textSubtitleColor,
      //                                       fontSize: 12.sp
      //                                     ),
      //                                   )
      //                                 ],
      //                               ),
      //                             )),
      //                           ],
      //                         ),
      //                       );
      //                     }).toList(),
      //                   ),
      //                   Container(
      //                     alignment: Alignment.centerRight,
      //                     child: Text.rich(
      //                       TextSpan(
      //                         text: '合计支付:  ',
      //                         style: TextStyle(
      //                           fontWeight: FontWeight.bold,
      //                           fontSize: 14.sp,
      //                           color: textBlackColor
      //                         ),
      //                         children: [
      //                           TextSpan(
      //                             text: '¥',
      //                             style: TextStyle(
      //                               fontWeight: FontWeight.bold,
      //                               fontSize: 12.sp,
      //                               color: textBlackColor
      //                             ),
      //                           ),
      //                           TextSpan(
      //                             text: '${item.totalAmount}',
      //                             style: TextStyle(
      //                               fontWeight: FontWeight.bold,
      //                               fontSize: 16.sp,
      //                               color: textBlackColor
      //                             ),
      //                           )
      //                         ]
      //                       )
      //                     ),
      //                   )
      //                 ],
      //               ),
      //             ),
      //             item.status! == 1 ?

      //             Container(
      //               height: 60.h,
      //               padding: EdgeInsets.symmetric(horizontal: 4.w),
      //               decoration: BoxDecoration(
      //                 border: Border(
      //                   top: BorderSide(
      //                     color: lineColor,
      //                     width: .5
      //                   )
      //                 )
      //               ),
      //               child: StoreOrderBtns(
      //                 status: item.status!, 
      //                 toPayFun: () async {
      //                   var res = await Get.toNamed(
      //                     '${AppRouters.COMMON_PAGE}${AppRouters.GOOD_DETAIL_PAGE}${AppRouters.STORE_CONFIRM_ORDER_PAGE}${AppRouters.STORE_ORDER_PAY_PAGE}',
      //                     arguments: {
      //                       'orderId': item.id,
      //                       'isDetail': false
      //                     }
      //                   );
      //                   if (res != null && res) {
      //                     widget.storeOrderPageController.getStoreTabItemOrderList(widget.storeOrderPageController.tabIndex.value);
      //                   }
      //                 },
      //                 cleanOrderFun: () {
      //                   widget.storeOrderPageController.storeOrderCancel(item.id!);
      //                 },
      //                 receiveOrderFun: () {},
      //                 logisticsFun: () {},
      //                 confirmReceiptFun: () {}
      //               )
      //             ) : Container(
      //               height: 60.h,
      //               padding: EdgeInsets.symmetric(horizontal: 4.w),
      //               decoration: BoxDecoration(
      //                 border: Border( 
      //                   top: BorderSide(
      //                     color: lineColor,
      //                     width: .5
      //                   )
      //                 )
      //               ),
      //               child: Row(
      //                 mainAxisAlignment: MainAxisAlignment.end,
      //                 children: [
      //                   Container(
      //                     width: 90.w,
      //                     child: ButtonUtil(
      //                       text: '查看订单', 
      //                       fontSize: 14.w, 
      //                       borderRadius: 18.w, 
      //                       height: 36.h, 
      //                       onTop: () {
      //                         Get.toNamed(
      //                           '${AppRouters.COMMON_PAGE}${AppRouters.STORE_ORDER_PAGE}${AppRouters.STORE_ORDER_DETAIL_PAGE}',
      //                           arguments: {
      //                             'orderId': item.id,
      //                             'title': StoreOrderStatusUtil.statusMap['${item.status}'],
      //                             'status': item.status
      //                           }
      //                         );
      //                       }
      //                     ),
      //                   )
      //                 ],
      //               )
      //             )
      //           ],
      //         ),
      //       ),
      //     ), () {
      //       Get.toNamed(
      //         '${AppRouters.COMMON_PAGE}${AppRouters.STORE_ORDER_PAGE}${AppRouters.STORE_ORDER_DETAIL_PAGE}',
      //         arguments: {
      //           'orderId': item.id,
      //           'title': StoreOrderStatusUtil.statusMap['${item.status}'],
      //           'status': item.status
      //         }
      //       );
      //     }),
      //   );
      // }).toList()
    );
  }

  @override
  bool get wantKeepAlive => true;
}